<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $color_header, $color_line1, $color_line2;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_md_title']);

//-------------------------------------------------------------------------------------------------

if (isset($_GET['defence_id']))
	$defence_id = $_GET['defence_id'];
elseif (isset($_POST['defence_id']))
	$defence_id = $_POST['defence_id'];
else
{
	echo $txt['bnt_md_invalid'], '<br>';
	TextMainMenu();
	return;
}

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($playerinfo['turns'] < 1)
{
	echo $txt['bnt_md_noturn'], '<br><br>';
	TextMainMenu();
	return;
}

// Pull sector info into $sectorinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector_id' => $playerinfo['sector']
	)
);
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

//Put the defence information into the array "defenceinfo"
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_sector_defence}
	WHERE defence_id = {int:defence_id}',
	array(
		'db_sector_defence' => $dbtables['sector_defence'],
		'defence_id' => $defence_id
	)
);

if ($smcFunc['db_num_rows']($result) == 0)
{
	echo $txt['bnt_md_nolonger'], '<br><br>';
	TextMainMenu();
	return;
}

$defenceinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($defenceinfo['sector_id'] <> $playerinfo['sector'])
{
	echo $txt['bnt_md_nothere'], '<br><br>';
	TextMainMenu();
	return;
}

if ($defenceinfo['ship_id'] == $playerinfo['ship_id'])
	$defence_owner = $txt['bnt_md_you'];
else
{
	$defence_ship_id = $defenceinfo['ship_id'];
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE ship_id = {int:defence_ship_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'defence_ship_id' => $defence_ship_id
		)
	);
	$ownerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	$defence_owner = $ownerinfo['character_name'];
}

$defence_type = $defenceinfo['defence_type'] == 'F' ? $txt['bnt_fighters'] : $txt['bnt_mines'];
$qty = $defenceinfo['quantity'];

if ($defenceinfo['fm_setting'] == 'attack')
{
	$set_attack = 'CHECKED';
	$set_toll = '';
}
else
{
	$set_attack = '';
	$set_toll = 'CHECKED';
}

// Get query string variables
$response = (isset($_POST['response'])) ? $_POST['response'] : '';
$quantity = (isset($_POST['quantity'])) ? $_POST['quantity'] : '';
$mode = (isset($_POST['mode'])) ? $_POST['mode'] : '';

if ($response == 'fight')
{
	if ($defenceinfo['ship_id'] == $playerinfo['ship_id'])
	{
		echo $txt['bnt_md_yours'], '<br><br>';
		TextMainMenu();
		return;
	}
	
	$sector = $playerinfo['sector'] ;
	
	if($defenceinfo['defence_type'] == 'F')
	{
		$result = $smcFunc['db_query']('', '
			SELECT SUM(quantity) AS totalfighters
			FROM {raw:db_sector_defence}
			WHERE sector_id = {int:sector}
			AND defence_type = "F"',
			array(
				'db_sector_defence' => $dbtables['sector_defence'],
				'sector' => $sector
			)
		);
		$ttl = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		
		$total_sector_fighters = $ttl['totalfighters'];
		$calledfrom = 'modify_defences';
		include("sector_fighters.php");
	}
	else
	{
		// Attack mines goes here
		$result = $smcFunc['db_query']('', '
			SELECT SUM(quantity) AS totalmines
			FROM {raw:db_sector_defence}
			WHERE sector_id = {int:sector}
			AND defence_type = "M"',
			array(
				'db_sector_defence' => $dbtables['sector_defence'],
				'sector' => $sector
			)
		);
		$ttl = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		
		$total_sector_mines = $ttl['totalmines'];
		
		$playerbeams = NUM_BEAMS($playerinfo['beams']);
		
		if ($playerbeams > $playerinfo['ship_energy'])
			$playerbeams = $playerinfo['ship_energy'];
		
		if ($playerbeams > $total_sector_mines)
			$playerbeams=$total_sector_mines;
		
		echo $txt['bnt_md_bmines'], ' ', $playerbeams, ' ', $txt['bnt_mines'], '<br>';
		
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET ship_energy = ship_energy - {int:playerbeams}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'playerbeams' => $playerbeams,
				'ship_id' => $playerinfo['ship_id']
			)
		);
		
		explode_mines($sector,$playerbeams);
		
		$char_name = $playerinfo['character_name'];
		
		$txt['bnt_md_msgdownerb'] = str_replace("[sector]", $sector, $txt['bnt_md_msgdownerb']);
		$txt['bnt_md_msgdownerb'] = str_replace("[mines]", $playerbeams, $txt['bnt_md_msgdownerb']);
		$txt['bnt_md_msgdownerb'] = str_replace("[name]", $char_name, $txt['bnt_md_msgdownerb']);
		message_defence_owner($sector,"$txt[bnt_md_msgdownerb]");
	}
}
elseif ($response == 'retrieve')
{
	if ($defenceinfo['ship_id'] <> $playerinfo['ship_id'])
	{
		echo $txt['bnt_md_notyours'], '<br><br>';
		TextMainMenu();
		return;
	}
	
	$quantity = stripnum($quantity);
	
	if ($quantity < 0)
		$quantity = 0;

	if ($quantity > $defenceinfo['quantity'])
		$quantity = $defenceinfo['quantity'];

	$torpedo_max = NUM_TORPEDOES($playerinfo['torp_launchers']) - $playerinfo['torps'];
	$fighter_max = NUM_FIGHTERS($playerinfo['computer']) - $playerinfo['ship_fighters'];
    
	if ($defenceinfo['defence_type'] == 'F')
	{
		if ($quantity > $fighter_max)
			$quantity = $fighter_max;
	}

	if ($defenceinfo['defence_type'] == 'M')
	{
		if ($quantity > $torpedo_max)
			$quantity = $torpedo_max;
	}

	$ship_id = $playerinfo['ship_id'];
    
	if ($quantity > 0)
	{
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_sector_defence}
			SET quantity = quantity - {int:quantity}
			WHERE defence_id = {int:defence_id}',
			array(
				'db_sector_defence' => $dbtables['sector_defence'],
				'quantity' => $quantity,
				'defence_id' => $defence_id
			)
		);
		
		if ($defenceinfo['defence_type'] == 'M')
		{
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET torps = torps + {int:quantity}
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'quantity' => $quantity,
					'ship_id' => $ship_id
				)
			);
		}
		else
		{
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET ship_fighters = ship_fighters + {int:quantity}
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'quantity' => $quantity,
					'ship_id' => $ship_id
				)
			);
		}
		
		$delete = $smcFunc['db_query']('', '
			DELETE FROM {raw:db_sector_defence}
			WHERE quantity <= 0',
			array(
				'db_sector_defence' => $dbtables['sector_defence'],
				'quantity' => $quantity
			)
		);	
	}
	
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET last_login = NOW(),
			turns = turns - 1,
			turns_used = turns_used + 1,
			sector = {int:sector}
		WHERE ship_id = {int:ship_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'sector' => $playerinfo['sector'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
	
	echo $txt['bnt_md_retr'], ' ', $quantity, ' ', $defence_type, '.<br>';
}
elseif ($response == 'change')
{
	if ($defenceinfo['ship_id'] <> $playerinfo['ship_id'])
	{
		echo $txt['bnt_md_notyours'], '<br><br>';
		TextMainMenu();
		return;
	}

	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_sector_defence}
		SET fm_setting = {string:mode}
		WHERE defence_id = {int:defence_id}',
		array(
			'db_sector_defence' => $dbtables['sector_defence'],
			'mode' => $mode,
			'defence_id' => $defence_id
		)
	);
	
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET last_login = NOW(),
			turns = turns - 1,
			turns_used = turns_used + 1,
			sector = {int:sector}
		WHERE ship_id = {int:ship_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'sector' => $playerinfo['sector'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
	
	if ($mode == 'attack')
		$mode = $txt['bnt_md_attack'];
	else
		$mode = $txt['bnt_md_toll'];
		
	$txt['bnt_md_mode'] = str_replace("[mode]", $mode, $txt['bnt_md_mode']);
	echo $txt['bnt_md_mode'], '<br>';
}
else
{
	$txt['bnt_md_consist'] = str_replace("[qty]", $qty, $txt['bnt_md_consist']);
	$txt['bnt_md_consist'] = str_replace("[type]", $defence_type, $txt['bnt_md_consist']);
	$txt['bnt_md_consist'] = str_replace("[owner]", $defence_owner, $txt['bnt_md_consist']);
	echo $txt['bnt_md_consist'], '<br><br>';

	if ($defenceinfo['ship_id'] == $playerinfo['ship_id'])
	{
		echo '
		<form action="', $scripturl, '?action=blacknova;sa=modify_defences" method="post">
		', $txt['bnt_md_retrieve'], ' <input type="test" name="quantity" size="10" maxlength="10" value="0"></input> ', $defence_type, '<br><br>
		<input type="hidden" name="response" value="retrieve">
		<input type="hidden" name="defence_id" value=', $defence_id, '>
		<input type="submit" value="', $txt['bnt_submit'], '"><br><br><br>
		</form>';
		 
		if ($defenceinfo['defence_type'] == 'F')
		{
            echo '
			', $txt['bnt_md_change'], '<br><br>
			<form action="', $scripturl, '?action=blacknova;sa=modify_defences" method="post">
			', $txt['bnt_md_cmode'], ' :<br><br><input type="radio" style="check" name="mode" ', $set_attack, ' value="attack">', $txt['bnt_md_attack'], '</input><br>
			<input type="radio" style="check" name="mode" ', $set_toll, ' value="toll">', $txt['bnt_md_toll'], '</input><br><br>
			<input type="submit" value="', $txt['bnt_submit'], '"><br><br>
			<input type="hidden" name="response" value="change">
			<input type="hidden" name="defence_id" value="', $defence_id, '">
			</form>';
		}
	}
	else
	{
		$ship_id = $defenceinfo['ship_id'];
		
		$result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_ships}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
		$fighters_owner = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);

		if ($fighters_owner['team'] != $playerinfo['team'] || $playerinfo['team'] == 0)
		{
			echo '
			<form action="', $scripturl, '?action=blacknova;sa=modify_defences" method="post">
			', $txt['bnt_md_attdef'], '<br><input type="submit" value="', $txt['bnt_md_attack'], '"></input><br>
			<input type="hidden" name="response" value="fight">
			<input type="hidden" name="defence_id" value="', $defence_id, '">
			</form><br>';
		}
	}
}


//-------------------------------------------------------------------------------------------------

TextMainMenu();
?>
